
HASCOLOR = $(shell if test `which colorgcc`; then echo true; else echo false; fi)
ifneq ($(HASCOLOR),true)
	export CC=@printf "\033[34m   CC   $$@\033[0m\n" && gcc
else
	export CC=@printf "\033[34m   CC   $$@\033[0m\n" && colorgcc
endif
AR=@printf "\033[32m   AR   $$@\033[0m\n" && ar
LD=@printf "\033[31m   LD   $$@\033[0m\n" && ld

export CPPFLAGS=
export CFLAGS=-Wall -O3 #-g
export CFLAGS_DEBUG=-g
LDFLAGS=
SUBDIRS=array_list linked_queue linked_stack
OBJ=obj
LIB=lib
DOC=doc
LIBNAME=libdsc

.PHONY: all clean clean-doc clean-tests default doc Makefile mrproper test

default: all 

force_look:
	@true

all: ${LIB}/$(LIBNAME).a ${LIB}/$(LIBNAME).so ${LIB}/$(LIBNAME).dll

${LIB}/$(LIBNAME).a: force_look
	@for i in $(SUBDIRS); do \
		printf "\033[1m>>> [$$i]\033[0m\n"; \
		$(MAKE) -s -C $$i build_static; \
		if [ $$? = 0 ]; then printf "\033[1m<<< [$$i] [OK]\033[0m\n"; else printf "\033[31m\033[1m<<< [$$i] [FAIL]\033[0m\n"; exit 1; fi; \
	done
	$(AR) r $@ $(OBJ)/*.o
	make clean

${LIB}/$(LIBNAME).so: force_look
	@for i in $(SUBDIRS); do \
		printf "\033[1m>>> [$$i]\033[0m\n"; \
		$(MAKE) -s -C $$i build_so; \
		if [ $$? = 0 ]; then printf "\033[1m<<< [$$i] [OK]\033[0m\n"; else printf "\033[31m\033[1m<<< [$$i] [FAIL]\033[0m\n"; exit 1; fi; \
	done
	$(CC) -shared ${OBJ}/*.o -o $@

${LIB}/$(LIBNAME).dll: force_look
	@for i in $(SUBDIRS); do \
		printf "\033[1m>>> [$$i]\033[0m\n"; \
		$(MAKE) -s -C $$i build_dll; \
		if [ $$? = 0 ]; then printf "\033[1m<<< [$$i] [OK]\033[0m\n"; else printf "\033[31m\033[1m<<< [$$i] [FAIL]\033[0m\n"; exit 1; fi; \
	done
	$(CC) -shared ${OBJ}/*.o -o $@
	make clean

doc:
	doxygen doxyfile

clean:
	rm -f ${OBJ}/*.o

clean-doc:
	rm -rf ${DOC}/html

clean-tests:
	rm -f ${TEST}/*.o ${TEST}/*.exe ${TEST}/*.gch

mrproper: clean clean-doc clean-tests
	rm -f ${LIB}/*
